import { Controller, Get, Post, Body, Patch, Param, Delete, Query } from '@nestjs/common';
import { CourseOfferingService } from './course_offering.service';
import { CreateCourseOfferingDto } from './dto/create-course_offering.dto';
import { UpdateCourseOfferingDto } from './dto/update-course_offering.dto';
import { ApiOperation, ApiResponse } from '@nestjs/swagger';
import { QueryCourseOfferingDto } from './dto/query-course-offering.dto';

@Controller('course_offering')
export class CourseOfferingController {
  constructor(private readonly courseOfferingService: CourseOfferingService) {}

  @Post()
  @ApiOperation({ summary: '创建开课记录' })
  @ApiResponse({ status: 201, description: '创建成功' })
  create(@Body() createCourseOfferingDto: CreateCourseOfferingDto) {
    return this.courseOfferingService.create(createCourseOfferingDto);
  }

  @Get()
  @ApiOperation({ summary: '获取开课列表' })
  @ApiResponse({ status: 200, description: '获取成功' })
  findAll(@Query() query: QueryCourseOfferingDto) {
    return this.courseOfferingService.findAll(query);
  }

  @Get('teacher/:tno')
  @ApiOperation({ summary: '获取教师开课列表' })
  @ApiResponse({ status: 200, description: '获取成功' })
  findByTeacher(
    @Param('tno') tno: string,
    @Query('semester_id') semester_id?: string
  ) {
    return this.courseOfferingService.findByTeacher(+tno, semester_id ? +semester_id : undefined);
  }

  @Get('semester/:semester_id')
  @ApiOperation({ summary: '获取学期开课列表' })
  @ApiResponse({ status: 200, description: '获取成功' })
  findBySemester(@Param('semester_id') semester_id: string) {
    return this.courseOfferingService.findBySemester(+semester_id);
  }

  @Get(':id')
  @ApiOperation({ summary: '获取单个开课记录' })
  @ApiResponse({ status: 200, description: '获取成功' })
  findOne(@Param('id') id: string) {
    return this.courseOfferingService.findOne(+id);
  }

  @Patch(':id')
  @ApiOperation({ summary: '更新开课记录' })
  @ApiResponse({ status: 200, description: '更新成功' })
  update(
    @Param('id') id: string,
    @Body() updateCourseOfferingDto: UpdateCourseOfferingDto
  ) {
    return this.courseOfferingService.update(+id, updateCourseOfferingDto);
  }

  @Patch(':id/status')
  @ApiOperation({ summary: '更新课程状态' })
  @ApiResponse({ status: 200, description: '更新成功' })
  updateStatus(
    @Param('id') id: string,
    @Body('status') status: 'open' | 'closed' | 'cancelled'
  ) {
    return this.courseOfferingService.updateStatus(+id, status);
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除开课记录' })
  @ApiResponse({ status: 200, description: '删除成功' })
  remove(@Param('id') id: string) {
    return this.courseOfferingService.remove(+id);
  }
}
